package wangjg.coco.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;


public class CharsetMain {

	private String path;
	private String srcCharset;
	private String targetCharset;
	private String extName;
	
	private int _fileTotal;
	private int _relative_path;
	private int _hiddenFiles;
	private int _cantRead;
	private int _unmatchedFiles;
	private int _failedFiles;
	private int _handledFiles;
	
	private Map<String, String> infoRecord = new HashMap<String, String>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CharsetMain app = new CharsetMain();
		app.setExtName("java");
		app.setSrcCharset("UTF-8");
		app.setTargetCharset("GBK");
		app.setPath("E:\\temp\\xx");
		app.go();
		System.out.println(app.getMessage());
	}

	public void go() {
		this.recursion(new File(this.path));
	}

	private void recursion(File file){
		if(file.isFile()){
			this.handle(file);
		}else if(file.isDirectory()){
			File[] files = file.listFiles();
			for(File f:files){
				recursion(f);
			}
		}
	}
	
	private void handle(File srcFile) {
		_fileTotal++;
		if(!srcFile.isAbsolute()){
			this._relative_path++;
			return;
		}
		if(srcFile.isHidden()){
			this._hiddenFiles++;
			return;
		}
		if(!srcFile.canRead()){
			this._cantRead++;
			return;
		}
		if(this.extName!=null && !this.extName.equals(getExtName(srcFile.getName()))){
			this._unmatchedFiles++;
			return;
		}
		
		File outFile =  new File(srcFile.getPath()+".tmp");
		boolean success = this.trans(srcFile, outFile);
		if(success){
			srcFile.delete();
			outFile.renameTo(srcFile);
			this._handledFiles++;
		}else{
			outFile.deleteOnExit();
			this._failedFiles++;
		}
	}
	
	private boolean trans(File file, File outFile){
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(file);
			InputStreamReader reader = new InputStreamReader(in, srcCharset);
			
			out = new FileOutputStream(outFile);
			OutputStreamWriter writer = new OutputStreamWriter(out, targetCharset);
			
			char[] cbuf = new char[1024];
			int len = -1;
			while((len = reader.read(cbuf))!=-1){
				writer.write(cbuf, 0, len);
			}
			
			reader.close();
			writer.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			try {
				if(in!=null)in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(out!=null)out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static String getExtName(String fileName){
		String ext = "";
		int idx = (fileName==null? -1 : fileName.lastIndexOf("."));
		if(idx > -1){
			ext = fileName.substring(idx+1);
		}
		return ext;
	}
	
	public void setPath(String path) {
		this.path = path;
	}

	public void setSrcCharset(String srcCharset) {
		this.srcCharset = srcCharset;
	}

	public void setTargetCharset(String targetCharset) {
		this.targetCharset = targetCharset;
	}

	public void setExtName(String extName) {
		this.extName = extName;
	}

	public String getMessage() {
		String str = "参数 [srcCharset=" + srcCharset
				+ ", targetCharset=" + targetCharset + ", extName=" + extName
				+ ", path=" + path + "]\n";
		str += "统计[_fileTotal=" + _fileTotal + ", _relative_path="
				+ _relative_path + ", _hiddenFiles=" + _hiddenFiles
				+ ", _cantRead=" + _cantRead + ", _unmatchedFiles="
				+ _unmatchedFiles + ", _failedFiles=" + _failedFiles
				+ ", _handledFiles=" + _handledFiles+"]\n";
		return str;
	}

}
